' Strict
'SuperStrict

' Files
'Import "BPR.Widget.bmx"

' TWindow
Type TWindow Extends TWidget
	Field textSizeY:Int
	
	Field dragging:Int
	Field dragDistX:Int, dragDistY:Int
	
	' Init
	Method Init(nID:String, nText:String, nX:Int, nY:Int, nWidth:Int, nHeight:Int)
		Self.InitWidget(nID, nText, nX, nY, nWidth, nHeight)
		Self.dragDistX = 0
		Self.dragDistY = 0
	End Method
	
	' InitSkinPadding
	Method InitSkinPadding() 
		' TODO: Remove hardcoded stuff
		Self.skinPadding.x1 = 1
		Self.skinPadding.x2 = 1
		Self.skinPadding.y1 = gui.skin.window.n.height
		Self.skinPadding.y2 = 1
		'Self.UpdateScreenPosition()
	End Method
	
	' SetText
	Method SetText(nText:String)
		Self.text = nText
		Self.textSizeY = TextHeight(nText)
	End Method
	
	' Update
	Method Update()
		Self.hovered = False
		
		If PointInRect(TInputSystem.GetMouseX(), TInputSystem.GetMouseY(), Self.screenX, Self.screenY, Self.rWidth, Self.rHeight)
			' Drag
			If TInputSystem.GetMouseDown(1)
				If TInputSystem.GetMouseHit(1)
					Self.Focus()
				EndIf
				
				' TODO: Optimise
				If Self.dragging = 0 And TInputSystem.GetMouseHit(1) And (PointInRect(TInputSystem.GetMouseX(), TInputSystem.GetMouseY(), Self.rX, Self.rY, Self.rWidth, Self.skinPadding.y1) Or TInputSystem.GetKeyDown(KEY_LALT))
					Self.dragDistX = TInputSystem.GetMouseX() - Self.rX
					Self.dragDistY = TInputSystem.GetMouseY() - Self.rY
					Self.dragging = 1
				EndIf
			Else
				Self.dragging = 0
			EndIf
			
			' Main application won't get any mouse events
			If Self.HasFocus()
				TInputSystem.EraseMouseEvents()
				Self.hovered = True
			Else
				gui.eraseEvents = True
			EndIf
		EndIf
		
		If Self.dragging
			Self.SetPositionAbs((TInputSystem.GetMouseX() - Self.GetRelOffsetX()) - Self.dragDistX, (TInputSystem.GetMouseY() - Self.GetRelOffsetY()) - Self.dragDistY)
			
			' Main application won't get any mouse events
			TInputSystem.EraseMouseEvents()
		EndIf
	End Method
	
	' Draw
	Method Draw()
		If Self.HasFocus() = 0 Then
			gui.alpha = gui.alpha * gui.noFocusAlpha		' TODO: Remove Hardcoded
		EndIf
		
		' Alpha
		Local nAlpha:Float = gui.alpha * Self.alpha
		
		' Draw
		Local westWidth:Int = gui.skin.window.w.width
		Local northHeight:Int = gui.skin.window.n.height
		Local southHeight:Int = gui.skin.window.s.height
		Local eastWidth:Int = gui.skin.window.e.width
		Local northWestWidth:Int = gui.skin.window.nw.width
		Local northEastWidth:Int = gui.skin.window.ne.width
		Local southWestWidth:Int = gui.skin.window.sw.width
		Local southEastWidth:Int = gui.skin.window.se.width
		Local southWestHeight:Int = gui.skin.window.sw.height
		
		' Draw shadow
		If nAlpha = 1.0
			.SetColor 0, 0, 0
			.SetAlpha gui.shadowIntensity
			DrawImageRectTiled gui.skin.window.s, gui.shadowOffsetX + Self.rX + southWestWidth, gui.shadowOffsetY + Self.rY + Self.rHeight - southHeight, Self.rWidth - southWestWidth - southEastWidth, southHeight
			DrawImageRectTiled gui.skin.window.e, gui.shadowOffsetX + Self.rX + Self.rWidth - eastWidth, gui.shadowOffsetY + Self.rY + northHeight, eastWidth, Self.rHeight - northHeight - southHeight
			DrawImage gui.skin.window.ne, gui.shadowOffsetX + Self.rX + Self.rWidth, gui.shadowOffsetY + Self.rY
			DrawImage gui.skin.window.sw, gui.shadowOffsetX + Self.rX, gui.shadowOffsetY + Self.rY + Self.rHeight
			DrawImage gui.skin.window.se, gui.shadowOffsetX + Self.rX + Self.rWidth, gui.shadowOffsetY + Self.rY + Self.rHeight
			
			'DrawRect Self.rX + westWidth, Self.rY + northHeight, Self.rWidth - westWidth - eastWidth, Self.rHeight - northHeight - southHeight
		EndIf
		
		' Draw center
		.SetAlpha nAlpha
		.SetColor 255, 255, 255
		DrawImageRectTiled gui.skin.window.c, Self.rX + westWidth, Self.rY + northHeight, Self.rWidth - westWidth - eastWidth, Self.rHeight - northHeight - southWestHeight
		DrawImageRectTiled gui.skin.window.c, Self.rX + southWestWidth, Self.rY + Self.rHeight - southWestHeight, Self.rWidth - southWestWidth - southEastWidth, southWestHeight - southHeight
		
		' Draw edges
		' TODO: Optimize this
		DrawImageRectTiled gui.skin.window.n, Self.rX + northWestWidth, Self.rY, Self.rWidth - northWestWidth - northEastWidth, northHeight
		DrawImageRectTiled gui.skin.window.s, Self.rX + southWestWidth, Self.rY + Self.rHeight - southHeight, Self.rWidth - southWestWidth - southEastWidth, southHeight
		DrawImageRectTiled gui.skin.window.w, Self.rX, Self.rY + northHeight, westWidth, Self.rHeight - northHeight - southHeight
		DrawImageRectTiled gui.skin.window.e, Self.rX + Self.rWidth - eastWidth, Self.rY + northHeight, eastWidth, Self.rHeight - northHeight - southHeight
		
		' Draw corners
		.SetColor Self.r, Self.g, Self.b
		DrawImage gui.skin.window.nw, Self.rX, Self.rY
		DrawImage gui.skin.window.ne, Self.rX + Self.rWidth, Self.rY
		DrawImage gui.skin.window.sw, Self.rX, Self.rY + Self.rHeight
		DrawImage gui.skin.window.se, Self.rX + Self.rWidth, Self.rY + Self.rHeight
		
		.SetColor Self.textR, Self.textG, Self.textB
		DrawText Self.text, Self.rX + gui.skin.window.textOffsetX, Self.rY + gui.skin.window.textOffsetY
	End Method
	
	' DrawEnd
	Method DrawEnd()
		If Self.HasFocus() = 0 Then
			gui.alpha = gui.alpha / gui.noFocusAlpha		' TODO: Remove Hardcoded
		EndIf
	End Method
	
	' Create
	Function Create:TWindow(nID:String, nText:String, nX:Int = 0, nY:Int = 0, nWidth:Int = 0, nHeight:Int = 0)
		Local widget:TWindow = New TWindow
		widget.Init(nID, nText, nX, nY, nWidth, nHeight)
		Return widget
	End Function
End Type
